package com.example.ah_geen_pda.ui.chipfrontsectionin

import androidx.databinding.ObservableArrayList
import com.example.ah_geen_pda.R
import com.example.ah_geen_pda.base.BaseViewModel
import com.example.ah_geen_pda.entity.*
import com.example.ah_geen_pda.model.LotInfoRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

class ChipFrontSectionInViewModel : BaseViewModel() {
    var lotItems = ObservableArrayList<LotEntity>()
    var componentItems = ObservableArrayList<ComponentList>()

    var lotItemBinding =
        ItemBinding.of<LotEntity>(BR.trackInItem, R.layout.item_track_in)
    val lotRepository: LotInfoRepository = LotInfoRepository()
    var lotDataBinding =
        ItemBinding.of<ComponentList>(BR.dataItem, R.layout.item_lot_list)
    fun getLotInfo(lotid: String,b:Boolean) {
        val map = JsonObject()
        if (b){
            map.addProperty("ACTIONTYPE","LOTINFOBYDURABLE")
            map.addProperty("DURABLE",lotid)
        }else{
            map.addProperty("ACTIONTYPE","LOTINFOBYID")
            map.addProperty("LOTID",lotid)
        }
        launchOnlyResult({
            val lotEntity = lotRepository.getLotInfo(map)
            if (lotEntity.LOT.STATE != "WAIT"){
                defUI.toastEvent.value =
                    ToastMessageBean("当前批次状态为"+lotEntity.LOT.STATE+",无法进站",false)
                return@launchOnlyResult
            }

            componentItems.addAll(lotEntity.LOT.COMPONENTLIST)
            lotEntity.LOT.start = lotEntity.LOT.COMPONENTLIST[0].ATTRIBUTE1
            lotEntity.LOT.end = lotEntity.LOT.COMPONENTLIST[lotEntity.LOT.COMPONENTLIST.size-1].ATTRIBUTE1
            lotItems.add(lotEntity)
        })
    }

    fun getLotCodeInfo(lotid: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","LOTINFOBYID")
        map.addProperty("LOTID",lotid)

        launchOnlyResult({
            val lotEntity = lotRepository.getLotInfo(map)
            lotEntity.LOT.COMPONENTLIST.forEach{
                componentItems.add(it)
            }

        })
    }

    fun checkChange(ischeck : Boolean){
        lotItems.forEach {
            it.LOT.showCheck = ischeck
        }
    }

    fun checkTechnicalListChange(ischeck : Boolean){
        componentItems.forEach {
            it.mShowCheck = ischeck
        }
    }

    fun trackIn(eqpID:String,carrierId:String) {
        var lotList : ArrayList<LotMessage> = arrayListOf()
        var lotCodeList : ArrayList<ComponentList> = arrayListOf()
        lotItems.forEach {
            if (it.LOT.STATE != "WAIT"){
                defUI.toastEvent.value =
                    ToastMessageBean("批次"+it.LOT.LOTID+"状态为"+it.LOT.STATE+",无法进站",false)
                return
            }
            if (it.LOT.showCheck){
                val lotMessage = LotMessage(it.LOT.LOTID)
                lotList.add(lotMessage)
            }
        }
        if (lotList.isEmpty()){
            defUI.toastEvent.value = ToastMessageBean("未发现选中批次", false)
            return
        }
        componentItems.forEach {
            if (it.mShowCheck){
                lotCodeList.add(it)
            }
        }
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","DefaultTrackIn")
        map.addProperty("EQUIPMENTID",eqpID)
        map.addProperty("CARRIERID",carrierId)
        map.addProperty("ISCHECKSTEPAUTHORITY",true)
        map.add("LOTLIST", JsonParser().parse(Gson().toJson(lotList)).asJsonArray)
        map.add("COMPONENTLIST", JsonParser().parse(Gson().toJson(lotCodeList)).asJsonArray)
        launchOnlyResult({
            lotRepository.defaultTrackIn(map)
            defUI.toastEvent.value = ToastMessageBean("进站成功", true)
            defUI.startActivity.call()
        })

    }


}